﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dart
{
    class DartGame
    {
        public const int GAME_1001 = 1;
        public const int GAME_501 = 2;
        public const int GAME_301 = 3;
        public const int GAME_101 = 4;

        public const int MODE_COUNT_UP = 1;
        public const int MODE_COUNT_DOWN = 2;

        public const int OPTION_IN_SINGLE = 1;
        public const int OPTION_IN_DOUBLE = 2;
        public const int OPTION_IN_TRIPLE = 3;
        public const int OPTION_IN_MASTER = 4;

        public const int OPTION_OUT_SINGLE = 1;
        public const int OPTION_OUT_DOUBLE = 2;
        public const int OPTION_OUT_TRIPLE = 3;
        public const int OPTION_OUT_MASTER = 4;

        private int cGame;
        private int cMode;
        private int cOptionIn;
        private int cOptionOut;

        private Dictionary<String, DartPlayer> dartPlayers = new Dictionary<String, DartPlayer>();
        private List<string> playerNames = new List<string>();

        private int playerCount;

        private Form parentForm;


        // **************************************************************
        // Constructor
        // **************************************************************
        public DartGame(Form parentForm)
        {
            this.SetGame(GAME_501);
            this.SetMode(MODE_COUNT_DOWN);
            this.playerCount = 0;
            this.parentForm = parentForm;
        }

        // **************************************************************
        // GAME
        // **************************************************************
        public void SetGame(int game)
        {
            cGame = game;
        }
        
        public int GetGame()
        {
            return cGame;
        }

        public String GetGameName(int game)
        {
            if (game == 1) return "1001";
            else if (game == 2) return "501";
            else if (game == 3) return "301";
            else if (game == 4) return "101";
            else return "";
        }

        public String GetGameName()
        {
            return this.GetGameName(this.GetGame());
        }

        public int GetGameScore()
        {
            if (this.GetGame() == 1) return 1001;
            else if (this.GetGame() == 2) return 501;
            else if (this.GetGame() == 3) return 301;
            else if (this.GetGame() == 4) return 101;
            else return 0;
        }

        // **************************************************************
        // MODE
        // **************************************************************
        public void SetMode(int mode)
        {
            cMode = mode;
        }
        
        public int GetMode()
        {
            return cMode;
        }

        private String GetModeName(int mode)
        {
            if (cMode == 1) return "Count Up";
            else if (cMode == 2) return "Count Down";
            else return "";

        }

        private String GetModeName()
        {
            return this.GetModeName(this.GetMode());
        }

        // **************************************************************
        // OPTION IN
        // **************************************************************
        public void SetOptionIn(int option)
        {
            cOptionIn = option;
        }

        public int GetOptionIn()
        {
            return cOptionIn;
        }

        private String GetOptionInName(int option)
        {
            if (cOptionIn == 1) return "Single In";
            else if (cOptionIn == 2) return "Double In";
            else if (cOptionIn == 3) return "Triple In";
            else if (cOptionIn == 4) return "Master In";
            else return "";

        }

        private String GetOptionInName()
        {
            return this.GetOptionInName(this.GetOptionIn());
        }

        // **************************************************************
        // OPTION OUT
        // **************************************************************
        public void SetOptionOut(int option)
        {
            cOptionOut = option;
        }

        public int GetOptionOut()
        {
            return cOptionOut;
        }

        private String GetOptionOutName(int option)
        {
            if (cOptionOut == 1) return "Single Out";
            else if (cOptionOut == 2) return "Double Out";
            else if (cOptionOut == 3) return "Triple Out";
            else if (cOptionOut == 4) return "Master Out";
            else return "";

        }

        private String GetOptionOutName()
        {
            return this.GetOptionOutName(this.GetOptionOut());
        }

        // **************************************************************
        // PLAYER NAMES
        // **************************************************************
        public void SetPlayerNames(List<string> list)
        {
            playerNames = list;
        }

        // **************************************************************
        // REFRESH
        // **************************************************************
        public void RefreshGameDisplay()
        {
            this.parentForm.Text = this.GetGameName() + " / " + this.GetModeName() + " / " + this.GetOptionInName() + " / " + this.GetOptionOutName();
        }

        // **************************************************************
        // GET PLAYER COUNT
        // **************************************************************
        public int GetPlayerCount()
        {
            return this.playerCount;
        }

        // **************************************************************
        // ADD PLAYER
        // **************************************************************
        public DartPlayer AddPlayer(String name, int score)
        {
            this.dartPlayers.Add(name, new DartPlayer(this.playerCount + 1, name, score, this.parentForm));
            DartPlayer dartPlayer = this.dartPlayers[name];

            Panel p = (Panel)this.parentForm.Controls["panelRight"];
            ComboBox comboBox = (ComboBox)p.Controls["comboBoxPlayer"];
            List<String> mem = new List<String>();
            mem = (List<String>)comboBox.DataSource;
            mem.Remove(name);
            comboBox.Invalidate();
            comboBox.DataSource = null;
            comboBox.DataSource = mem;
            comboBox.Refresh();

            dartPlayer.GetPlayerNameLabel().DoubleClick += new System.EventHandler(this.RemovePlayer);
            
            this.playerNames.Add(name);

            this.playerCount += 1;

            return dartPlayer;
        }

        // **************************************************************
        // GET PLAYER
        // **************************************************************
        public DartPlayer GetPlayer(String name)
        {
            return this.dartPlayers[name];
        }

        // **************************************************************
        // REFRESH PLAYERS
        // **************************************************************
        public void RefreshPlayers()
        {
            List<String> mem = new List<String>();

            foreach (DartPlayer dartPlayer in this.dartPlayers.Values)
            {
                this.parentForm.Controls.Remove(dartPlayer.GetPlayerNameLabel());
                this.parentForm.Controls.Remove(dartPlayer.GetPlayerScoreLabel());
                mem.Add(dartPlayer.GetName());
            }

            this.dartPlayers.Clear();
            this.playerCount = 0;

            foreach (String playerName in mem)
            {
                this.AddPlayer(playerName, this.GetGameScore());
            }
        }

        // **************************************************************
        // REMOVE PLAYER
        // **************************************************************
        private void RemovePlayer(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            DartPlayer dartPlayer = this.GetPlayer(l.Text);
            this.parentForm.Controls.Remove(dartPlayer.GetPlayerNameLabel());
            this.parentForm.Controls.Remove(dartPlayer.GetPlayerScoreLabel());
            this.playerNames.Remove(l.Text);
            this.playerNames.Sort();

            this.dartPlayers.Remove(dartPlayer.GetName());

            //ComboBox comboBox = (ComboBox)this.parentForm.Controls["comboBoxPlayer"];
            Panel p = (Panel)this.parentForm.Controls["panelRight"];
            //Panel p = (Panel)tlp.Controls["panelRight"];
            ComboBox comboBox = (ComboBox)p.Controls["comboBoxPlayer"];
            List<String> mem = new List<String>();
            mem = (List<String>)comboBox.DataSource;
            mem.Add(l.Text);
            mem.Sort();
            comboBox.Invalidate();
            comboBox.DataSource = null;
            comboBox.DataSource = mem;
            comboBox.Refresh();

            this.RefreshPlayers();
        }
    }
}
